package com.ww.juc.completable.future.handle;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

/**
 * 跟whenComplete基本一致，区别在于handle的回调方法有返回值
 * @author wanggw
 * 2024年2月4日 下午6:05:30
 */
public class HandleDemo {

	@SuppressWarnings("unused")
	public static void main(String[] args) throws InterruptedException, ExecutionException {
		CompletableFuture<Integer> cf1 = CompletableFuture.supplyAsync(() -> {
			System.out.println(Thread.currentThread() + "任务，谈笑风生中...");
			int a = 1 / 0;
			return 1;
		});
		
		CompletableFuture<Integer> cf2 = cf1.handle((result, e) -> {
			System.out.println(Thread.currentThread() + "任务2也开始做事干活了...");
			System.out.println("上个任务结果：" + result);
			System.out.println("上个任务抛出异常：" + e);
			return result + 2;
		});
		
		System.out.println("总的执行结果：" + cf2.get());
	}
}
